package com.tz.colawallet.ui.activity;

import android.Manifest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tz.colawallet.R;
import com.tz.colawallet.basecode.base.BaseActivity;
import com.tz.colawallet.basecode.comm.RetrofitHelp;
import com.tz.colawallet.bean.MessageEvent;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.ui.biz.MainBiz;
import com.tz.colawallet.ui.component.DaggerMainComponent;
import com.tz.colawallet.ui.contract.MainContract;
import com.tz.colawallet.ui.fragment.MainFirstFrag;
import com.tz.colawallet.ui.fragment.MainSecondFrag;
import com.tz.colawallet.ui.fragment.MainThirdFrag;
import com.tz.colawallet.ui.module.MainModule;
import com.tz.colawallet.ui.presenter.MainPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseActivity implements MainContract.View {

    @Inject
    MainPresenter mainPresenter;
    @Inject
    MainBiz mainBiz;

    long time;

    FragmentManager fm;
    Fragment cf;//记录当前fragment
    String ct;//当前tag

    public final static String FIRST = "首页";
    public final static String SECOND = "贷款";
    public final static String THIRD = "我的";

    FrameLayout mainFrameLayout;
    ImageView firstImg;
    TextView firstText;
    LinearLayout firstLayout;
    ImageView secondImg;
    TextView secondText;
    LinearLayout secondLayout;
    ImageView thirdImg;
    TextView thirdText;
    LinearLayout thirdLayout;

    private Fragment cluefra;
    private Fragment ownerfra;
    private Fragment minefra;

    @Override
    protected void setup() {
        super.setup();
        DaggerMainComponent.builder().mainModule(new MainModule(this)).build().inject(this);
        mainPresenter.setBiz(mainBiz);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }


    @Override
    protected void initTitle() {

    }

    @Override
    protected void init() {
        mainFrameLayout = findViewById(R.id.main_frame_layout);
        firstImg = findViewById(R.id.first_img);
        firstText = findViewById(R.id.first_text);
        firstLayout = findViewById(R.id.first_layout);
        secondImg = findViewById(R.id.second_img);
        secondText = findViewById(R.id.second_text);
        secondLayout = findViewById(R.id.second_layout);
        thirdImg = findViewById(R.id.third_img);
        thirdText = findViewById(R.id.third_text);
        thirdLayout = findViewById(R.id.third_layout);


        fm = getSupportFragmentManager();
        cluefra = fm.findFragmentByTag(FIRST);
        ownerfra = fm.findFragmentByTag(SECOND);
        minefra = fm.findFragmentByTag(THIRD);

        if (cluefra == null) {
            cluefra = new MainFirstFrag();
        }
        changFra(cluefra, FIRST);
        changeBottom(FIRST);

        firstLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cluefra == null) {
                    cluefra = new MainFirstFrag();
                }
                changFra(cluefra, FIRST);
                changeBottom(FIRST);
            }
        });

        secondLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ownerfra == null) {
                    ownerfra = new MainSecondFrag();
                }
                changFra(ownerfra, SECOND);
                changeBottom(SECOND);
            }
        });

        thirdLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (minefra == null) {
                    minefra = new MainThirdFrag();
                }
                changFra(minefra, THIRD);
                changeBottom(THIRD);
            }
        });

        setPermission();
    }

    private void setPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
                , Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void changeBottom(String tag) {
        firstImg.setImageResource(R.mipmap.first2);
        firstText.setTextColor(getResources().getColor(R.color.gray));
        secondImg.setImageResource(R.mipmap.second2);
        secondText.setTextColor(getResources().getColor(R.color.gray));
        thirdImg.setImageResource(R.mipmap.third2);
        thirdText.setTextColor(getResources().getColor(R.color.gray));

        switch (tag) {
            case FIRST:
                firstImg.setImageResource(R.mipmap.first1);
                firstText.setTextColor(getResources().getColor(R.color.main_color));
                break;
            case SECOND:
                secondImg.setImageResource(R.mipmap.second1);
                secondText.setTextColor(getResources().getColor(R.color.main_color));
                break;
            case THIRD:
                thirdImg.setImageResource(R.mipmap.third1);
                thirdText.setTextColor(getResources().getColor(R.color.main_color));
                break;
            default:
                break;
        }
    }

    private void changFra(Fragment fra, String tag) {
        ct = tag;
        if (fm.findFragmentByTag(tag) == null) {
            if (cf != null) {
                fm.beginTransaction().hide(cf).add(R.id.main_frame_layout, fra, tag).commit();
            } else {
                fm.beginTransaction().add(R.id.main_frame_layout, fra, tag).commit();
            }
            cf = fra;
            return;
        }
        if (fra.isHidden()) {
            if (cf != null) {
                fm.beginTransaction().hide(cf).show(fra).commit();
            } else {
                fm.beginTransaction().show(fra).commit();
            }
            cf = fra;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - time <= 2000L) {
                showToast("我退出APP啦");
                RetrofitHelp.logout();
                finish();
                System.exit(0);
            } else {
                showToast("再按一次退出程序");
                time = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.getEventKey()) {
            case Key.EVENT_TO_MAIN_SECOND:
                if (ownerfra == null) {
                    ownerfra = new MainSecondFrag();
                }
                changFra(ownerfra, SECOND);
                changeBottom(SECOND);
                break;
            default:
                break;
        }
    }
}
